package com.sanhuo.security.dao.provider;

import com.sanhuo.security.bo.UserUpdateBO;
import com.sanhuo.triild.provider.SQL;
import com.sanhuo.utils.basic.ReflectionUtil;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserProvider extends SQL {

    public String update(UserUpdateBO bo) throws IllegalAccessException {
        SQL sql = getSQL();
        sql.update("user");
        List<String> conditionList = new ArrayList<>();
        Field[] fields = ReflectionUtil.getFieldList(bo);
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.get(bo) != null) {
                conditionList.add(buildCondition(field.getName()));
            }
        }
        String[] condition = conditionList.toArray(new String[0]);
        sql.set(condition);
        sql.where(buildCondition("id"));
        return sql.build();
    }

}
